package cs.ui250.carnatic;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class RagaByNameActivity extends Activity {

	private EditText mSearchEdit;
	private ListView mRagaList;
	private TextView mNoResultsText;

	private ArrayList<String> mRagas;
	private ArrayList<Integer> mRagaIDs;
	private ArrayAdapter<String> mAdapter;

	private Context mContext;
	private DBAdapter mDB;

	private final int RAGA_FROM_BY_NAME = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.raga_by_name);

		mContext = getApplicationContext();
		mDB = DBAdapter.getInstance(mContext);
		mRagas = new ArrayList<String>();
		mRagaIDs = new ArrayList<Integer>();
		mAdapter = new ArrayAdapter<String>(this, R.layout.list_item, mRagas);
		mNoResultsText = (TextView) findViewById(R.id.rbn_empty_text);
		Button mClearButton = (Button) findViewById(R.id.rbn_clear_button);
		mClearButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mSearchEdit.setText("");
				mRagaIDs.clear();
				mRagas.clear();
				mAdapter.notifyDataSetChanged();
			}
		});
		mSearchEdit = (EditText) findViewById(R.id.rbn_search_edit);
		mSearchEdit.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				updateList();
			}
		});
		mRagaList = (ListView) findViewById(R.id.rbn_raga_list);
		mRagaList.setAdapter(mAdapter);
		mRagaList.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView parent, View v, int position,
					long id) {
				Intent intent = new Intent(RagaByNameActivity.this,
						RagaDisplay.class);
				intent.putExtra("RAGA_ID_KEY", mRagaIDs.get(position));
				startActivityForResult(intent, RAGA_FROM_BY_NAME);
			}
		});
	}

	private void updateList() {
		String input = mSearchEdit.getText().toString();
		mRagaIDs.clear();
		mRagas.clear();
		if (!input.equals("")) {
			Cursor cursor = mDB.ragaByName(mSearchEdit.getText().toString());
			startManagingCursor(cursor);
			if (cursor.moveToFirst()) {
				do {
					mRagaIDs.add(cursor.getInt(0));
					mRagas.add(cursor.getString(1) + " (" + cursor.getInt(0)
							+ ")");
				} while (cursor.moveToNext());
			}
		}
		if (mRagas.isEmpty()) {
			mNoResultsText.setText("No Results");
		} else {
			mNoResultsText.setText("");
		}
		mAdapter.notifyDataSetChanged();
	}
}
